package spe.ui.editor;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.gef.GraphicalViewer;
import org.eclipse.gef.RootEditPart;
import org.eclipse.gef.editparts.ScalableFreeformRootEditPart;
import org.eclipse.gef.ui.palette.PaletteViewer;
import org.eclipse.gef.ui.parts.ScrollingGraphicalViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.SashForm;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorSite;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.part.EditorPart;

/**
 * @author: Dao Anh Vu
 * @description: 
 * 
 * */
public class GraphEditorPage extends EditorPart 
{
	private SPEEditor editor;
	private MyScrollingGraphicalViewer viewer;
	private SPEOutlineViewer outlineViewer;
	
	public GraphEditorPage(SPEMGraphEditor editor)
	{
		super();
		this.editor = editor;
	}
	
	@Override
	public void createPartControl(Composite parent) 
	{
		SashForm sashForm = new SashForm(parent, SWT.HORIZONTAL);
		addPalette(sashForm);
		addModelViewer(sashForm);
		addOutlineViewer();
		sashForm.setWeights(new int[] {15, 85});
	}
	
	@Override
	public void doSave(IProgressMonitor arg0) 
	{
	}

	@Override
	public void doSaveAs() {
	}

	@Override
	public void init(IEditorSite site, IEditorInput input) throws PartInitException 
	{
		setSite(site);
		setInput(input);
	}

	@Override
	public boolean isDirty() {
		return false;
	}

	@Override
	public boolean isSaveAsAllowed() {
		return false;
	}
	
	private void addPalette(Composite composite)
	{
		PaletteViewer paletteViewer = new PaletteViewer();
		paletteViewer.createControl(composite);
		editor.getEditDomain().setPaletteViewer(paletteViewer);
		editor.getEditDomain().setPaletteRoot(new SPEMEditorPalette(editor));
	}
	
	private void addModelViewer(Composite composite)
	{
		viewer = editor.createGraphicalViewer();
		viewer.initControl(composite);
		getSite().setSelectionProvider(viewer);
	}
	
	private void addOutlineViewer() {
		outlineViewer = editor.createOutlineViewer();
	}
	
	public SPEOutlineViewer getOutlineViewer()
	{
		return outlineViewer;
	}
	
	public MyScrollingGraphicalViewer getGraphicalViewer()
	{
		return viewer;
	}
	
	public SPEEditor getEditor()
	{
		return editor;
	}
	
	@Override
	public void setFocus() 
	{
		//viewer.getControl().setFocus();
	}

}
